<?php
require_once APPLICATION_PATH . '/controllers/LoginController.php';

/**
 * ACL管理控制器
 * @author Administrator
 *
 */
class AclController extends LoginController
{

    public function init()
    {
        parent::init();
    }

    /**
     * 列表
     * @see LoginController::indexAction()
     */
    public function indexAction()
    {    	
    	$privilegeM = new Application_Model_DbTable_Privilege();
        if($this->getRequest()->isPost()){
        	//保存数据
        	$acl = $this->getRequest()->getParam("acl");
        	
        	$privilegeM->delete(" `action_id` =3 ");
        	foreach ($acl as $item){
        		$temp = explode(",", $item);
        		$item2 = array(
        			"user_id" 		=> $temp[0],
        			"resource_id" 	=> $temp[1],
        			"action_id"		=> 3	//“执行”动作
        		);        		
        		$privilegeM->insert($item2);
        	}
        }
        	//获取页面
    		$this->view->title = "ACL列表";
    		$resourceM 	= new Application_Model_DbTable_Resource();
    		$userM 		= new Application_Model_DbTable_User();
    		
    		$resources 	= $resourceM->fetchAll();
    		$users 		= $userM->fetchAll();
    		$privileges = $privilegeM->fetchAll(" `action_id` =3 ");
    		
    		$arr1 = array();
    		$arr2 = array();
    		
    		foreach ($resources as $r){
    			if($r->deep == 0){
    				$arr1[$r->id] = array(
    					"title" => $r->title,
    					"childrens"=> 0
    				);
    			}
    		}
        	foreach ($resources as $r){
    			if($r->deep == 1){
    				$arr2[$r->id] = $r->title;
    				$arr1[$r->pid]["childrens"] += 1;
    			}
    		}
    		
    		$rsPrivileges = array();
    		foreach ($privileges as $p){
    			$rsPrivileges[$p->user_id . "_" . $p->resource_id] = TRUE;
    		}
    		
    		$this->view->header1 	= $arr1;
    		$this->view->header2 	= $arr2;
    		$this->view->users 		= $users;
    		$this->view->privilege 	= $rsPrivileges;        	
    }
}

